<template>
    <div class="tableList">
        <el-table :data="tableData" border style="width: 100%;"
            :header-cell-style="{background:'#fafafa',color:'#000',fontWeight:400,fontSize:'12px'}"
            :cell-style="{color:'#5a5a5a',fontSize:'12px',fontWeight:400,padding:'5px 0'}" v-loading="loading"
            element-loading-text="加载中" element-loading-background="rgba(250, 250, 250, 0.5)"
            element-loading-spinner="el-icon-loading" row-key="id">
            <el-table-column label="序号" width="80">
                <template slot-scope="scope">
                    <span>{{(searchForm.page - 1) * (searchForm.limit) + scope.$index + 1}}</span>
                </template>
            </el-table-column>
            <el-table-column v-for="(item,index) in tableGroup" :key="index" :label="item.name" :min-width="item.width"
                show-overflow-tooltip>
                <template slot-scope="scope">
                    <span v-if="item.label == 'fieldType'">{{enumProp[scope.row[item.label]]}}</span>
                    <span v-if="item.label != 'fieldType'">
                        {{scope.row[item.label]}}
                    </span>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="180">
                <template slot-scope="scope">
                    <el-button type="text" size="small"
                        @click="edit(scope.row.id)">编辑
                    </el-button>
                    <el-popconfirm placement='top' confirm-button-text='删除' @confirm="getFieldDel({id:scope.row.id})"
                        title="确认删除字段？操作将同时删除历史数据，删除后不可恢复">
                        <el-button slot="reference" type="text" size="small" style="margin-left:10px">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
            <div slot="empty">
                <el-empty :image-size="100"></el-empty>
            </div>
        </el-table>
        <el-pagination v-if="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :page-sizes="[10,20,50,100]" :page-size="searchForm.limit" layout="total,prev, pager, next,sizes"
            :total="total" background>
        </el-pagination>
    </div>
</template>

<script>
    import {
        fieldList,
        fieldDel
    } from '@/api/customFieldController'
    import {customSortTable} from "@/mixin"
    export default {
        data() {
            return {
                tableData: [],
                loading: false,
                total: 0,
                enumProp:{
                    ipt:'文本',
                    inputNumber:'数字',
                    datePicker:'时间',
                    upload:'附件',
                    picture:'图片',
                    selectRadio:'单选框',
                    selectChecked:'复选框',
                    relevanceObject:'关联对象',
                    relevanceProp:'关联属性'
                },
                searchForm: {
                    name: 'stock',
                    page: 1,
                    limit: 10
                }
            }
        },
        props: {
            tableGroup: {
                type: Array,
                default: () => []
            },
        },
        mounted() {
        },
        mixins:[customSortTable],
        methods: {
            //自定义字段删除
            getFieldDel(data) {
                let totalPage = Math.ceil((this.total - 1) / this.searchForm.limit)
                let currentPage = this.searchForm.page > totalPage ? totalPage : this.searchForm.page
                this.searchForm.page = currentPage < 1 ? 1 : currentPage
                fieldDel(data).then(res => {
                   if(res.code == 200){
                        this.getFieldList(this.searchForm)
                        this.utils.showMsg('删除成功', 'success')
                   }
                })
            },

            //编辑
            edit(id){
                this.$router.push({
                    path:'/customFields/inventoryManagement/edit',
                    query:{
                        id
                    }
                })
            },
            //pageSize 改变时会触发
            handleSizeChange(val) {
                this.searchForm.limit = val
                this.getFieldList(this.searchForm)
            },
            //currentPage 改变时会触发
            handleCurrentChange(val) {
                this.searchForm.page = val
                this.getFieldList(this.searchForm)
            },
        },
    }
</script>

<style lang="less" scoped>
    .tableList {
        padding: 0 20px;
    }
</style>